﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TAB04TP
    {
        #region ==PC===========
        public static readonly DependencyProperty PCProperty = DependencyProperty.Register("PC",
        typeof(string), typeof(TAB04TP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPCChanged));
        public string PC
        {
            get
            {
                return (string)GetValue(PCProperty);
            }
            set
            {
                SetValue(PCProperty, value);
            }
        }
        private static void OnPCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB04TP;
            if (src != null)
            {
                src.PC = (string)e.NewValue;
            }
        }
        #endregion

        public TAB04TP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABPOS();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwAB = dwMaster;
            if (dwMaster == null)
            {
                QTY = 0;

            }
            else
            {
                QTY = System.Convert.ToDouble(dwMaster["ABPTP"].ToString());
            }
            //此时规则和AB52应当进行了装载
        }        
        public override void OnABTPChg(string value)
        {
            if (value.Trim() == "")
                return;
            QTY =double.Parse( value.Substring(8 + 10+5,3));
            base.OnABTPChg(value);
        }
        public int dy = 0;

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                dwData["ABPMANDANT"] = app.Mandant;
                dwData["ABPAUFKRS"] = ABTP.Substring(0, 2);
                dwData["ABPAUFLFD"] = ABTP.Substring(2, 6);
                dwData["ABPBESCHICH"] = ABTP.Substring(8, 10);
                dwData["ABPTP"] =double.Parse(ABTP.Substring(18, 4))+ QTY;
                TMV.UABP171(dwData);
                base.NewCommand(sender, e);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void PrintCommand(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            dwData = TBMain.NewRow();
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"] = ABTP.Substring(0, 2);
            dwData["ABPAUFLFD"] = ABTP.Substring(2, 6);
            dwData["ABPBESCHICH"] = " AND ABPBESCHICH='" + ABTP.Substring(8, 10) + "'";
            SResult = TMV.QAB04FN(dwData);
            Set.Merge(SResult);
            cDR.PRINT(Set);
        }

        public void PrintAFrameCard(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataSet SetResult = new DataSet();
            dwData = TBMain.NewRow();
            dwData["ABPFILLER1"] = EXAUF;
            dwData["WBBTP"] = EXAUF + PC;
            PM["KRS"] = EXAUF.Substring(0, 2);
            PM["LFD"] = EXAUF.Substring(2, 6);
            PM["POS"] = EXAUF.Substring(8, 3);
            SResult = TMV.VABF.LOADABF(PM);
            SetResult = TMV.TXT.QTXT06(dwData);
            Set.Merge(SetResult);
            Set.Merge(SResult);
            Set.Merge(TBMain);
            cDR2.PRINT(Set);
        }

        public void PrintCom(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            DataSet Set = new DataSet();
            int tq = 0;
            tq = int.Parse(PC);
            if (tq <= 0) return;
            if (tq > QTY)
            {
                MessageBox.Show("补打批号必须是已产生的批号！");
                return;
            }
            string ARTICLE = "", WID = "", FIN = "", TRA = "", ART = "", DES = "", ABPAUFKRS = "", ABPAUFLFD = "", ABPAUFPOS = "";
            ABPAUFKRS =  EXAUF.Substring(0, 2);
            ABPAUFLFD =  EXAUF.Substring(2, 6);
            ABPAUFPOS = EXAUF.Substring(8, 3);
            ARTICLE = dwAB["ARTICLE"].ToString();
            WID = dwAB["WID"].ToString();
            FIN = dwAB["FIN"].ToString();
            TRA = dwAB["TRA"].ToString();
            ART = dwAB["ART"].ToString();
            DES = dwAB["DES"].ToString();
            TBMain.Clear();
            dwData = TBMain.Rows.Add();
            dwData["WBBTP"] = EXAUF + PC;
            dwData["ABPAUFKRS"] = ABPAUFKRS;
            dwData["ABPAUFLFD"] = ABPAUFLFD;
            dwData["ABPAUFPOS"] = ABPAUFPOS;
            dwData["ARTICLE"] = ARTICLE;
            dwData["WID"] = WID;
            dwData["FIN"] = FIN;
            dwData["TRA"] = TRA;
            dwData["ART"] = ART;
            dwData["DES"] = DES;
            Set.Merge(TBMain);
            cDR1.PRINT(Set);
            OnEXAUFChg(EXAUF);
        }
    }
}